﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using CLDataAppManagement;
using ControlzEx.Standard;
using DryIoc;

namespace CL_VisionCommu_Test.Helper
{
    public static class CommuConfiguration
    {
        static DataAppManagement dataAppManagement = new DataAppManagement();

        public static void LoadIni(string Path, out int ErrorCode)
        {
            ErrorCode = -1;
            string ErrorMsg = "Load File Error";
            try
            {
                dataAppManagement.LoadFile(Path, out ErrorCode, out ErrorMsg);
            }
            catch (Exception)
            {
                System.Windows.MessageBox.Show("ErrorCode:" + ErrorCode.ToString() + " ErrorMsg:" + ErrorMsg);
            }
        }

        public static string GetValueByPath(string Path, out int ErrorCode)
        {
            string Value = "";
            ErrorCode = -1;
            string ErrorMsg = "GetValueByPath Error";
            try
            {
                string buffer = dataAppManagement.GetToken(Path, out ErrorCode, out ErrorMsg).ToString().Split(':')[1];
                Value = GetStringValue(buffer);
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK);
            }
            return Value;
        }

        public static List<CommunicationParameters> GetParameters()
        {
            int count = Convert.ToInt32(CommuConfiguration.GetValueByPath("Root.Global.ObjectManagers", out int Errocode));
            List<CommunicationParameters> parameters = new List<CommunicationParameters>();
            for (int i = 0; i < count; i++)
            {
                CommunicationParameters para = new CommunicationParameters();
                para.Name = "ObjectManager" + i.ToString() + "-Interface0";
                para.ObjectCount = Convert.ToInt32(CommuConfiguration.GetValueByPath("Root.Global.UserDefObjects", out Errocode));
                para.InterfaceDLL = CommuConfiguration.GetValueByPath("Root." + para.Name + ".InterfaceDLL", out Errocode);
                para.Ip = CommuConfiguration.GetValueByPath("Root." + para.Name + ".Ip", out Errocode);
                para.Port = int.Parse(CommuConfiguration.GetValueByPath("Root." + para.Name + ".Port", out Errocode));
                //para.Timeout= int.Parse(CommuConfiguration.GetValueByPath("Root." + para.Name + ".Timeout", out Errocode));
                //para.BufferSize = int.Parse(CommuConfiguration.GetValueByPath("Root." + para.Name + ".BufferSize", out Errocode));
                //para.TraceToDebugger = int.Parse(CommuConfiguration.GetValueByPath("Root." + para.Name + ".TraceToDebugger", out Errocode));
                //para.TraceLevel= int.Parse(CommuConfiguration.GetValueByPath("Root." + para.Name + ".TraceLevel", out Errocode));
                //para.TraceWriteThreadID= int.Parse(CommuConfiguration.GetValueByPath("Root." + para.Name + ".TraceWriteThreadID", out Errocode));
                //para.InheritTrace= int.Parse(CommuConfiguration.GetValueByPath("Root." + para.Name + ".InheritTrace", out Errocode));
                //para.IgnoreCRC= int.Parse(CommuConfiguration.GetValueByPath("Root." + para.Name + ".IgnoreCRC", out Errocode));
                //para.Device= int.Parse(CommuConfiguration.GetValueByPath("Root." + para.Name + ".Device", out Errocode));
                para.Objects = new ObservableCollection<ObjectItems>();
                for (int a = 0; a < para.ObjectCount; a++)
                {
                    ObjectItems objects = new ObjectItems();
                    objects.ObjectID = int.Parse(CommuConfiguration.GetValueByPath("Root.Object" + a.ToString() + ".ObjectID", out Errocode));
                    objects.ItemsCount = int.Parse(CommuConfiguration.GetValueByPath("Root.Object" + a.ToString() + ".Items", out Errocode));
                    objects.Name = CommuConfiguration.GetValueByPath("Root.Object" + a.ToString() + ".Name", out Errocode) + "_" + objects.ObjectID.ToString();
                    objects.Items = new ObservableCollection<ItemDefined>();
                    for (int c = 0; c < objects.ItemsCount; c++)
                    {
                        ItemDefined item = new ItemDefined();
                        item.No = c;
                        item.Type = CommuConfiguration.GetValueByPath("Root.Object" + a.ToString() + ".Item" + c.ToString() + ".Type", out Errocode);
                        item.Array = int.Parse(CommuConfiguration.GetValueByPath("Root.Object" + a.ToString() + ".Item" + c.ToString() + ".Array", out Errocode));
                        item.Default = CommuConfiguration.GetValueByPath("Root.Object" + a.ToString() + ".Item" + c.ToString() + ".Default", out Errocode);
                        if (item.Array > 1)
                        {
                            string[] str = new string[item.Array];
                            for (int index = 0; index < item.Array; index++)
                            {
                                str[index] = item.Default;
                            }
                            item.Default = "[" + string.Join(",", str) + "]";
                        }
                        item.Name = CommuConfiguration.GetValueByPath("Root.Object" + a.ToString() + ".Item" + c.ToString() + ".Name", out Errocode);
                        item.MaxSize = int.Parse(CommuConfiguration.GetValueByPath("Root.Object" + a.ToString() + ".Item" + c.ToString() + ".MaxSize", out Errocode));
                        //item.ByteLength= int.Parse(CommuConfiguration.GetValueByPath("Root.Object" + a.ToString() + ".Item" + c.ToString() + ".ByteLength", out Errocode));
                        objects.Items.Add(item);
                    }
                    para.Objects.Add(objects);
                }
                parameters.Add(para);
            }
            return parameters;
        }

        private static string GetStringValue(string str)
        {
            string result = str;
            if (str.Contains("\""))
            {
                int startIndex = str.IndexOf("\"") + 1;
                int endIndex = str.IndexOf("\"", startIndex);
                if (startIndex > 0 && endIndex > startIndex)
                {
                    result = str.Substring(startIndex, endIndex - startIndex);
                }
            }
            return result;
        }
    }
}
